<?php

namespace App\Filament\Resources;

use App\Filament\Resources\CategoryResource\Pages;
use App\Filament\Resources\CategoryResource\RelationManagers;
use App\Models\Category;
use Filament\Forms;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class CategoryResource extends Resource
{
    protected static ?string $model = Category::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?int $navigationSort = 2;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.article_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.article_group.category');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.article_group.category');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('name')->label(__('category.Name'))->required(),
                Textarea::make('description')->label(__('category.Description'))
                    ->rows(6),
                TextInput::make('sort')->integer()->label(__('category.Sort'))
                    ->default(1)
                    ->required(),
                Radio::make('status')
                    ->label(__('category.Status'))
                    ->options([
                        '0' => '隐藏',
                        '1' => '显示',
                    ])
                    ->inline()
                    ->inlineLabel(false)
                    ->default('1')
                    ->required(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')
                    ->label(__('category.Id'))
                    ->alignCenter(),
                TextColumn::make('name')
                    ->label(__('category.Name'))
                    ->limit(50)
                    ->badge()
                    ->searchable(),
                TextColumn::make('description')
                    ->label(__('category.Description'))
                    ->color('gray')
                    ->limit(50)
                    ->wrap(),
                TextColumn::make('sort')
                    ->label(__('category.Sort'))
                    ->alignCenter(),
                TextColumn::make('status')
                    ->label(__('category.Status'))
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        '0' => 'gray',
                        '1' => 'success',
                    })
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        '0' => '隐藏',
                        '1' => '显示',
                    })
                    ->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListCategories::route('/'),
        ];
    }
}
